package com.crwal.app.utils;


import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;

public class JsoupUtil {

    private static Log logger = LogFactory.getLog(JsoupUtil.class);

    private static int sleep = 10000;

    private static int timeout = 60000;
    public static String USER_AGENT = "User-Agent";
    public static String USER_AGENT_VALUE = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134";
    //
    //Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
    private static Response connect(String url, Method method, Map<String, String> data, Map<String, String> cookies) {
        int retry = 5;// 重试次数
        Response resp = null;
        while (retry > 0) {
            try {
                Connection conn = Jsoup.connect(url).method(method).header(USER_AGENT, USER_AGENT_VALUE)
                		/*.header("content-type", "application/x-www-form-urlencoded; charset=UTF-8")
                		.header("accept-encoding", "gzip, deflate, br")
                		.header("accept-language", "zh-CN,zh;q=0.9")
                		.header("cache-control", "no-cache")
                		.header("referer", "https://www.cjbio.net/en/news/news/list.do")
                		.header("x-csrf-token", "db7239e7-1fea-48d6-9270-7efed3481416")
                		.header("x-requested-with", "XMLHttpRequest")
                		.header("sec-fetch-dest", "empty")
                		.header("sec-fetch-mode", "cors")
                		.header("sec-fetch-site", "same-origin")*/
                		.ignoreContentType(true)
                        .timeout(timeout);
                if (null != data) {
                    conn.data(data);
                }
                if (null != cookies) {
                    conn.cookies(cookies);
                }
                resp = conn.execute();
                break;
            } catch (Exception e) {
                logger.error("出现异常，重试：" + retry);
                logger.error("url>" + url);
                logger.error("data>" + data);
                retry--;
                logger.error(e);

                // 出现异常，暂停sleep毫秒
                try {
                    Thread.sleep(sleep);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                e.printStackTrace();
            }

        }
        return resp;
    }

    public static Response post(String url, Map<String, String> data, Map<String, String> cookies) {
        return connect(url, Method.POST, data, cookies);
    }

    public static Response get(String url, Map<String, String> cookies) {
        return connect(url, Method.GET, null, cookies);
    }
    public static Response post(String url, Map<String, String> data) {
        return connect(url, Method.POST, data, null);
    }

    public static Response get(String url) {
        return connect(url, Method.GET, null, null);
    }

}
